package cn.yunhe.action.temp;

import cn.yunhe.pojo.House;
import cn.yunhe.service.HouseService;
import cn.yunhe.util.BaseAction;
import cn.yunhe.util.Pagination;
import com.opensymphony.xwork2.ModelDriven;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HouseAction3 extends BaseAction implements ModelDriven {
    private House house;
    private String target;
    private List<House> houseList=new ArrayList<>();


    private String hprice_range;
    private String hfloorage_range;
    private int street_district_did;
    private int street_sid;
    private int type_tid;
    private int currPageNo;
    private int totalRecords;

    private Pagination<House>pagination=new Pagination<>();

    HouseService houseService=new HouseService();

    /**
     * 插入
     * @return
     */
    public String insertHouse(){
        //receiveParameter2House(house);
        boolean flag=houseService.insertHouse(house);
        if (flag){
            return SUCCESS;
        }else {
            return ERROR;
        }
    }

    /**
     * 查询
     * @return
     */
    public String houseList(){
        house.getStreet().getDistrict().setDid(street_district_did);
        house.getStreet().setSid(street_sid);
        house.getType().setTid(type_tid);
        //核查接收的参数集合
        receiveParameter2House(house);

        int hprice2Left=-1;
        int hprice2Right=-1;
        int hfloorage2Left=-1;
        int hfloorage2Right=-1;
        if (hprice_range!=null){
            String[] hprice2Str=hprice_range.split("-");
            hprice2Left=Integer.parseInt(hprice2Str[0]);
            hprice2Right=Integer.parseInt(hprice2Str[1]);
        }
        if (hfloorage_range!=null){
            String[] hfloorage2Str=hfloorage_range.split("-");
            hfloorage2Left=Integer.parseInt(hfloorage2Str[0]);
            hfloorage2Right=Integer.parseInt(hfloorage2Str[1]);
        }

        Map<String,Object>map=new HashMap<>();
        map.put("htitle",house.getHtitle());
        map.put("hprice2Left",hprice2Left);
        map.put("hprice2Right",hprice2Right);
        map.put("hfloorage2Left",hfloorage2Left);
        map.put("hfloorage2Right",hfloorage2Right);
        map.put("street_district_did",street_district_did);
        map.put("street_sid",street_sid);
        map.put("type_tid",type_tid);
        System.out.println("111未添加分页参数"+map.values());


        //获取总记录数
        totalRecords=houseService.houseListByMap2TotalRecords(map);
        //翻页功能
        pagination.setTotalRecords(totalRecords);
        pagination.setMaxRows(3);
        pagination.setCurrPageNo(currPageNo);
        checkPagination();


        map.put("startIndex",pagination.getStartIndex());
        map.put("maxRows",pagination.getMaxRows());
        System.out.println("2222添加分页参数"+map.values());
        houseList=houseService.houseListByMap(map);
        target=SUCCESS;
        return target;
    }

    public House getHouse() {
        return house;
    }

    public void setHouse(House house) {
        this.house = house;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public List<House> getHouseList() {
        return houseList;
    }

    public void setHouseList(List<House> houseList) {
        this.houseList = houseList;
    }

    /**
     * 模型驱动,单级
     * @return
     */
    @Override
    public House getModel() {
        if (house==null)
            house=new House();
        return house;
    }

    /**
     * 测试使用
     *
     */
    public void receiveParameter2House(House house){
        System.out.println("hprice_range======="+hprice_range);
        System.out.println("hfloorage_range======="+hfloorage_range);
        System.out.println("street_district_did======="+street_district_did);
        System.out.println("street_sid======="+street_sid);
        System.out.println("type_tid======="+type_tid);
        System.out.println("currPageNo======="+currPageNo);
        System.out.println("==================Start==================");
        System.out.println("getHid======="+house.getHid());
        System.out.println("getHtitle======="+house.getHtitle());
        System.out.println("getHprice======="+house.getHprice());
        System.out.println("getHfloorage======="+house.getHfloorage());
        System.out.println("getHpubdate======="+house.getHpubdate());
        System.out.println("getHcontact======="+house.getHcontact());
        System.out.println("getHdesc======="+house.getHdesc());
        System.out.println("getHflag======="+house.getHflag());
        if (house.getType()!=null)
            System.out.println("getType().getTid()======="+house.getType().getTid());
            System.out.println("getType().getTname()======="+house.getType().getTname());
        if (house.getMember()!=null)
            System.out.println("getMember().getMid()======="+house.getMember().getMid());
            System.out.println("getMember().getMname()======="+house.getMember().getMname());
        if (house.getStreet()!=null)
            System.out.println("getStreet().getSid()======="+house.getStreet().getSid());
            System.out.println("getStreet().getSname()======="+house.getStreet().getSname());
        if (house.getStreet().getDistrict()!=null)
            System.out.println("getDistrict()======"+house.getStreet().getDistrict());
            System.out.println("getDistrict().getDid()======"+house.getStreet().getDistrict().getDid());
            System.out.println("getDistrict().getDname()======"+house.getStreet().getDistrict().getDname());
        System.out.println("===================End=================");
    }

    //核查翻页的参数
    public void checkPagination(){
        System.out.println("getTotalRecords======"+pagination.getTotalRecords());
        System.out.println("getMaxRows======"+pagination.getMaxRows());
        System.out.println("getCurrPageNo======"+pagination.getCurrPageNo());
        System.out.println("getPrePageNo======"+pagination.getPrePageNo());
        System.out.println("getNextPageNo======"+pagination.getNextPageNo());
        System.out.println("getTotalPages======"+pagination.getTotalPages());
        System.out.println("getStartIndex======"+pagination.getStartIndex());
        System.out.println("getKeyWords======"+pagination.getKeyWords());
        /*System.out.println("getList.size======"+pagination.getList().size());*/
        System.out.println();
    }


    public String getHprice_range() {
        return hprice_range;
    }

    public void setHprice_range(String hprice_range) {
        this.hprice_range = hprice_range;
    }

    public String getHfloorage_range() {
        return hfloorage_range;
    }

    public void setHfloorage_range(String hfloorage_range) {
        this.hfloorage_range = hfloorage_range;
    }

    public int getStreet_district_did() {
        return street_district_did;
    }

    public void setStreet_district_did(int street_district_did) {
        this.street_district_did = street_district_did;
    }

    public int getStreet_sid() {
        return street_sid;
    }

    public void setStreet_sid(int street_sid) {
        this.street_sid = street_sid;
    }

    public int getType_tid() {
        return type_tid;
    }

    public void setType_tid(int type_tid) {
        this.type_tid = type_tid;
    }

    public int getCurrPageNo() {
        return currPageNo;
    }

    public void setCurrPageNo(int currPageNo) {
        this.currPageNo = currPageNo;
    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    public Pagination<House> getPagination() {
        return pagination;
    }

    public void setPagination(Pagination<House> pagination) {
        this.pagination = pagination;
    }
}
